package com.emagic.manage.data.network.interceptors;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.emagic.manage.data.utils.AndroidUtil;
import com.emagic.manage.data.utils.Md5;
import com.emagic.manage.data.utils.Preconditions;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Headers;

/**
 * <Pre>
 * 请求参数头封装
 * </Pre>
 *
 * @author baoy
 * @version 1.0
 *          create by 15/7/16 下午2:38
 */
final class RequestParamsWrapper {

    private final static String tf = "yyMMddHHmmssSSS";

    private String method;           //接口类名
    private String reqtime;          //请求时间
    private String sign;             //签名
    private String version;          //版本
    private String ctype;            //客户端类型
    private String TAG =RequestParamsWrapper.class.getSimpleName();

    /**
     * 有请求参数的接口
     *
     * @param ctx context
     * @param api 接口方法名称
     */
    RequestParamsWrapper(Context ctx, String api) {

        Preconditions.checkArgument(ctx != null, "context cannot be null");
        Preconditions.checkArgument(!TextUtils.isEmpty(api), "api cannot be null");

        this.method = api;
        this.reqtime = getRequestTime();
        this.version = String.valueOf(AndroidUtil.getAppVersionCode(ctx));
        this.ctype = "android";

    }


    /**
     * 组装Post请求参数,包含请求头及数据体
     *
     * @param json
     * @return MAP
     */
    public Map<String, String> getFormParams(String json) {

        try {
            json = URLDecoder.decode(json, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Map<String, String> params = new HashMap<>();
        params.put("METHOD", method);
        params.put("RECDATA", json);
        return params;
    }

    /**
     * 组装Post请求参数,包含请求头及数据体
     *
     * @param recData
     * @return MAP
     */
    public Map<String, String> getFormParams(Map<String, String> recData) {
        return getFormParams(toJsonString(recData));
    }

    /**
     * 组装GET请求参数,包含请求头及数据体
     *
     * @param json
     * @return JSON串
     */
    public String getUrlParams(String json) {
        StringBuilder getParams = new StringBuilder("RECDATA=");
        try {
            json = URLEncoder.encode(json, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        getParams.append(json).append("&METHOD=").append(method);

        return getParams.toString();
    }

    /**
     * 组装GET请求参数,包含请求头及数据体
     *
     * @param recData
     * @return JSON串
     */
    public String getUrlParams(Map<String, String> recData) {
        Gson gson = new Gson();
        String json = gson.toJson(recData);
        return getUrlParams(json);
    }

    public Headers getRequestHeaders(Headers headers) {
        Headers.Builder builder = headers.newBuilder();
        builder.add("REQTIME", reqtime);
        builder.add("SIGN", sign);
        builder.add("VERSION", version);
        builder.add("CTYPE", ctype);
        Log.e(TAG, "getRequestHeaders: "+"-REQTIME:"+reqtime+"-SIGN:"+sign+"-VERSION"+version+"-CTYPE:"+ctype );
        return builder.build();
    }

    private String toJsonString(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    @SuppressLint("SimpleDateFormat")
    private static String getRequestTime() {

        Calendar cal = new GregorianCalendar();
        Date date = cal.getTime();

        SimpleDateFormat format = new SimpleDateFormat(tf);

        return format.format(date);
    }

    private String sign(String reqTime, String token) {
        return Md5.md5(token + reqTime);
    }
}
